package com.cyw.search.service.impl;

import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.domain.entity.SysUser;
import com.cyw.common.helper.LoginHelper;
import com.cyw.common.utils.StringUtils;
import com.cyw.search.entity.monents.Moments;
import com.cyw.search.entity.user.EsUser;
import com.cyw.search.mapper.EsUserMapper;
import com.cyw.search.mapper.MomentsMapper;
import com.cyw.search.service.SearchMomentsService;
import com.cyw.system.mapper.SysUserMapper;
import com.cyw.system.service.ISysOssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: 二叔
 * @Date: 2022/11/02/17:39
 */
@Slf4j
@Service
public class SearchMomentsServiceImpl implements SearchMomentsService {

    @Resource
    private MomentsMapper momentsMapper;

    @Resource
    private ISysOssService iSysOssService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private EsUserMapper esUserMapper;

    @Override
    public R publishMoments(String geoPoint, String content, MultipartFile[] multipartFiles) {
        List<String> listUrl = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            listUrl.add(iSysOssService.upload(multipartFile).getUrl());
        }
        SysUser sysUser = sysUserMapper.selectUserById(LoginHelper.getUserId());
        Moments moments = Moments.initPublish(content, listUrl, geoPoint, sysUser);
        Integer insert = momentsMapper.insert(moments);
        if (insert > 0){
            return R.ok("发布成功");
        }
        return R.fail("发布失败,请联系管理员");
    }

    @Override
    public PageInfo<Moments> searchNearMoments(String geoPoint,Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        GeoPoint centerPoint = new GeoPoint(geoPoint);
        wrapper.geoDistance(Moments::getLocation, 100.0, DistanceUnit.KILOMETERS, centerPoint)
            .orderByDistanceAsc(Moments::getLocation, centerPoint);
        return momentsMapper.pageQuery(wrapper, pageNum, pageSize);
    }

    @Override
    public PageInfo<Moments> searchAllMoments(Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.orderByDesc(Moments::getCreateTime);
        return momentsMapper.pageQuery(wrapper, pageNum, pageSize);
    }

    @Override
    public PageInfo<Moments> searchMyMoments(Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Moments::getUserId, LoginHelper.getUserId()).orderByDesc(Moments::getCreateTime);
        return momentsMapper.pageQuery(wrapper,pageNum,pageSize);
    }

    @Override
    public PageInfo<Moments> searchMomentsByContent(String searchContent, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        if (StringUtils.isNotBlank(searchContent)) {
          wrapper.queryStringQuery(searchContent);
        }
        return momentsMapper.pageQuery(wrapper,pageNum,pageSize);
    }

    @Override
    public String deleteMoments(String momentsId) {
        return momentsMapper.deleteById(momentsId)>0?"删除成功":"删除失败";
    }

    @Override
    public PageInfo<Moments> searchMomentsByUserId(Long userId, Integer pageNum, Integer pageSize) {
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Moments::getUserId, userId).orderByDesc(Moments::getCreateTime);
        return momentsMapper.pageQuery(wrapper, pageNum, pageSize);
    }

    @Override
    public PageInfo<Moments> searchFollowMoments(Integer pageNum, Integer pageSize) {
        EsUser esUser = esUserMapper.selectById(LoginHelper.getUserId());
        LambdaEsQueryWrapper<Moments> wrapper = new LambdaEsQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(esUser.getILoveSet())) {
            wrapper.in(Moments::getUserId, esUser.getILoveSet()).orderByDesc(Moments::getCreateTime);
        }else {
            wrapper.in(Moments::getUserId, 0).orderByDesc(Moments::getCreateTime);
        }
        return momentsMapper.pageQuery(wrapper, pageNum, pageSize);
    }
}
